FROM 172.20.7.86:5001/conda/miniconda-311

LABEL maintainer="Gaoyu <gaoyu.ssdut@icloud.com>"

# 设置环境变量
ENV PATH /root/miniconda3/bin:/root/miniconda3/condabin:$PATH
# 基础镜像中包含python3.11和常用包的conda环境
ENV CONDA_DEFAULT_ENV=dutwater11
ENV PATH /root/miniconda3/envs/dutwater11/bin:$PATH
RUN echo $PATH

# 激活conda环境
RUN echo "conda activate dutwater11" >> ~/.bashrc

# # 安装s3fs
# RUN apt-get install fuse -y
# RUN apt-get install s3fs -y
# # 挂载s3
# RUN echo "k0Rgmb6IbwlFnoyvhqnm:89xozgjydEWBURfcHJW9TwT0kGZZNK4a1mjWRz72" > ~/.passwd-s3fs
# RUN chmod 600 ~/.passwd-s3fs
# RUN mkdir -p ~/minio/grids-origin
# RUN s3fs grids-origin ~/minio/grids-origin -o use_path_request_style  -o passwd_file=~/.passwd-s3fs -o url=http://172.20.7.86:9000
# RUN mkdir -p ~/minio/gfs-origin
# RUN s3fs gfs-origin ~/minio/gfs-origin -o use_path_request_style  -o passwd_file=~/.passwd-s3fs -o url=http://172.20.7.86:9000

# 安装系统依赖（包括 OpenGL 相关库）
RUN apt-get update && apt-get install -y \
    libgl1-mesa-glx \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    && rm -rf /var/lib/apt/lists/*

# 工作目录
WORKDIR /app

# docker时区问题
# RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 设置conda镜像源
RUN conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ && \
    conda config --set show_channel_urls yes

# 检查 Python 版本
RUN python3 --version
RUN which python3

# 先复制requirements.txt文件
COPY requirements.txt /app/requirements.txt

# 使用conda安装依赖包
RUN conda install -y pip && \
    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 复制所有文件
COPY . /app

# 创建.env文件（因为.env在.gitignore中不会被复制）
RUN echo "# MGHydro Configuration Environment Variables" > /app/.env && \
    echo "# Copy this file and customize the values for your environment" >> /app/.env && \
    echo "" >> /app/.env && \
    echo "# Input/Output Paths" >> /app/.env && \
    echo "OUTLETS_CSV=outlets_sample.csv" >> /app/.env && \
    echo "OUTPUT_DIR=output" >> /app/.env && \
    echo "PICKLE_DIR=pkl" >> /app/.env && \
    echo "MAP_FOLDER=map" >> /app/.env && \
    echo "" >> /app/.env && \
    echo "# Data Directories" >> /app/.env && \
    echo "MERIT_FDIR_DIR=data/raster/flowdir_basins" >> /app/.env && \
    echo "MERIT_ACCUM_DIR=data/raster/accum_basins" >> /app/.env && \
    echo "HIGHRES_CATCHMENTS_DIR=data/shp/merit_catchments" >> /app/.env && \
    echo "LOWRES_CATCHMENTS_DIR=data/shp/catchments_simplified" >> /app/.env && \
    echo "RIVERS_DIR=data/shp/merit_rivers" >> /app/.env && \
    echo "" >> /app/.env && \
    echo "# Processing Options" >> /app/.env && \
    echo "HIGH_RES=True" >> /app/.env && \
    echo "VERBOSE=True" >> /app/.env && \
    echo "PLOTS=True" >> /app/.env && \
    echo "OUTPUT_CSV=True" >> /app/.env && \
    echo "FILL=True" >> /app/.env && \
    echo "SIMPLIFY=False" >> /app/.env && \
    echo "MAKE_MAP=True" >> /app/.env && \
    echo "MAP_RIVERS=True" >> /app/.env && \
    echo "MATCH_AREAS=False" >> /app/.env && \
    echo "" >> /app/.env && \
    echo "# Output Format" >> /app/.env && \
    echo "OUTPUT_EXT=gpkg" >> /app/.env && \
    echo "" >> /app/.env && \
    echo "# Thresholds and Parameters" >> /app/.env && \
    echo "LOW_RES_THRESHOLD=50000" >> /app/.env && \
    echo "SEARCH_DIST=0" >> /app/.env && \
    echo "FILL_THRESHOLD=100" >> /app/.env && \
    echo "SIMPLIFY_TOLERANCE=0.0008" >> /app/.env && \
    echo "NUM_STREAM_ORDERS=3" >> /app/.env && \
    echo "AREA_MATCHING_THRESHOLD=0.25" >> /app/.env && \
    echo "MAX_DIST=0.075" >> /app/.env && \
    echo "THRESHOLD_SINGLE=500" >> /app/.env && \
    echo "THRESHOLD_MULTIPLE=5000" >> /app/.env

# EXPOSE 8000

# 不执行任何启动命令，只保持容器运行
CMD ["tail", "-f", "/dev/null"]


# git (连接仓库)
# https://gitcode.com/dlut-water/mghydro.git

# image (使用conda构建)
# docker build -f docker_file/Dockerfile -t mghydro:conda .

# container (容器)
# docker run --name mghydro_stable -d --network=host -v ~/minio:/app/minio:rw mghydro:conda

# update (进入容器内更新)
# docker exec -ti mghydro_stable bash
# git pull

# 如果更新不下来
# git config --global user.name gaoyussdut
# git config --global user.email gaoyu.ssdut@icloud.com

# 添加 SSH 私钥
# echo '-----BEGIN OPENSSH PRIVATE KEY-----' > /root/.ssh/id_ed25519 && \
# echo 'b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW' >> /root/.ssh/id_ed25519 && \
# echo 'QyNTUxOQAAACA9wMmnFncu1tMwTqqF3mjiYpE/wJKaDd+RWp/tTPA1UQAAAKDEvxagxL8W' >> /root/.ssh/id_ed25519 && \
# echo 'oAAAAAtzc2gtZWQyNTUxOQAAACA9wMmnFncu1tMwTqqF3mjiYpE/wJKaDd+RWp/tTPA1UQ' >> /root/.ssh/id_ed25519 && \
# echo 'AAAEA/A6uuTXUkUbwkOQq+yprYXlnfGNzrmN2IWoQpoO7Ouz3AyacWdy7W0zBOqoXeaOJi' >> /root/.ssh/id_ed25519 && \
# echo 'kT/AkpoN35Fan+1M8DVRAAAAFmdhb3l1LnNzZHV0QGljbG91ZC5jb20BAgMEBQYH' >> /root/.ssh/id_ed25519 && \
# echo '-----END OPENSSH PRIVATE KEY-----' >> /root/.ssh/id_ed25519 && \
# chmod 600 /root/.ssh/id_ed25519

# 添加 SSH 公钥
# echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID3AyacWdy7W0zBOqoXeaOJikT/AkpoN35Fan+1M8DVR gaoyu.ssdut@icloud.com' > /root/.ssh/id_ed25519.pub && \
# chmod 644 /root/.ssh/id_ed25519.pub

# 更新
# git pull

# 检查新包是否安装成功
# conda list | grep -E "(geopandas|shapely|pandas)"